// pages/orderpaysucc/orderpaysucc.js
var that,
  api = new (require('../../utils/api.js')),
  headimg = api.getimgurl(),
  app = getApp(),
  util = new (require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    num: 4,//后端给的分数,显示相应的星星  
    one_1: '',  
    two_1: '',  
    one_2: 0,  
    two_2: 5,
    one_3: 0,  
    two_3: 5,
    one_4: 0,  
    two_4: 5,
    env:0,
    feel:0,
    station:0,
    code:1,
    showmod:!1,
  },
   /* 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this;
    console.log(options,'optionsoptions');
    that.setData({orderId:options.orderId,code:options.code},()=>{
      that.getdate()
    })
    //情况一:展示后台给的评分  
    this.setData({  
      one_1: this.data.num,  
      two_1: 5 - this.data.num  
    })  
  },
  getdate(){
    wx.showLoading({
      title: '加载中',
    })
    let {orderId,code}=that.data
    if(code=='1'){
      api.oildetail(orderId,res=>{
        if(res.data.code==0){
          if(res.data.entity.data.dealTag==1){
            let data=res.data.entity.data
            data.payFeearr=Number(data.payFee).toFixed(2).split('.')
            data.totalFeearr=Number(data.totalFee).toFixed(2).split('.')
            data.couponFeearr=Number(data.couponFee).toFixed(2).split('.')
            data.daySalearr=Number(data.daySale).toFixed(2).split('.')
            that.setData({data})
            api.oilgrantcoupon(orderId,res=>{
              wx.hideLoading()
              if(res.data.code==0){
                let {couponArr,num}=res.data.entity.data
                couponArr.forEach(x=>{
                  x.feearr=Number(x.fee).toFixed(2).split('.')
                })
                that.setData({couponnum:num,couponArr})
              }
            })
          }else{
            wx.showToast({
              title: '支付失败',
              icon: 'none',
              duration:1000,
            })
            setTimeout(function () {
              wx.navigateBack()
            }, 1000)
          }
        }else{
          wx.hideLoading()
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration:1000,
          })
          setTimeout(function () {
            wx.navigateBack()
          }, 1000)
        }
      })
      
    }else{
      api.goodsdetail(orderId,res=>{
        if(res.data.code==0){
          if(res.data.entity.data.dealTag==1){
            let data=res.data.entity.data
            data.payFeearr=Number(data.payFee).toFixed(2).split('.')
            data.totalFeearr=Number(data.totalFee).toFixed(2).split('.')
            data.couponFeearr=Number(data.couponFee).toFixed(2).split('.')
            data.daySalearr=Number(data.daySale).toFixed(2).split('.')
            that.setData({data})
            api.fypgrantcoupon(orderId,res=>{
              wx.hideLoading()
              if(res.data.code==0){
                let {couponArr,num}=res.data.entity.data
                couponArr.forEach(x=>{
                  x.feearr=Number(x.fee).toFixed(2).split('.')
                })
                that.setData({couponnum:num,couponArr})
              }
            })
          }else{
            wx.showToast({
              title: '支付失败',
              icon: 'none',
              duration:1000,
            })
            // setTimeout(function () {
            //   wx.navigateBack()
            // }, 1000)
          }
        }else{
          wx.hideLoading()
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration:1000,
          })
          setTimeout(function () {
            wx.navigateBack()
          }, 1000)
        }
      })
      
    }
    let posId=code=='1'?801:code=='2'?802:803
    api.bulkpopon(posId,res=>{
      if(res.data.code==0){
        if(res.data.entity.data){
          that.setData({bulkpopondata:res.data.entity.data,showmod:!0})
        }
      }
    })
    let { corpBulkTag } = app.logininfo
    that.setData({corpBulkTag})
  },
  in_xin:function(e){  
    var in_xin = e.currentTarget.dataset.in;  
    var one_2;  
    if (in_xin === 'use_sc2'){  
      one_2 = Number(e.currentTarget.id);  
    } else {  
      one_2 = Number(e.currentTarget.id) + that.data.one_2;  
    }  
    that.setData({  
      one_2: one_2,  
      two_2: 5 - one_2,
      env:Math.round(one_2*20)
    })  
  }  ,
  in_xin2:function(e){  
    var in_xin = e.currentTarget.dataset.in;  
    var one_3;  
    if (in_xin === 'use_sc2'){  
      one_3 = Number(e.currentTarget.id);  
    } else {  
      one_3 = Number(e.currentTarget.id) + this.data.one_3;  
    }  
    this.setData({  
      one_3: one_3,  
      two_3: 5 - one_3,
      feel:Math.round(one_3*20)
    })  
  }  ,
  in_xin3:function(e){  
    var in_xin = e.currentTarget.dataset.in;  
    var one_4;  
    if (in_xin === 'use_sc2'){  
      one_4 = Number(e.currentTarget.id);  
    } else {  
      one_4 = Number(e.currentTarget.id) + this.data.one_4;  
    }  
    this.setData({  
      one_4: one_4,  
      two_4: 5 - one_4,
      station:Math.round(one_4*20)
    })  
  }  ,
  //匿名评价
  anonymous(){
    let {one_2,one_3,one_4,data,station,feel,env}=that.data,err=''
    if(one_4==0)err="请为综合服务打分"
    if(one_3==0)err="请为消费环境打分"
    if(one_2==0)err="请为加油服务打分"
    if(err!=''){
      wx.showModal({
        content:err,
        showCancel:!1,
      })
      return;
    }
    api.reviewfeel({orderId:data.orderId,station,feel,env},res=>{
      if(res.data.code==0){
        wx.showToast({
          title: '评论成功',
          icon: 'none',
          duration:1000,
          success:res1=>{
            setTimeout(function () {
              wx.switchTab({url: '../index/index'})
              // wx.navigateBack()
            }, 1000)
          }
        })
      }else{
        wx.showToast({
          title: '评论失败',
          icon: 'none',
          duration:1000,
        })
      }
    })
  },
  //取消评论
  cancel(){
    wx.switchTab({url: '../index/index'})
  },
  togoodsdetail(e){
    let { id } = e.currentTarget.dataset
    wx.navigateTo({
      url: '../shopdetail/shopdetail?id=' + id,
    })
  },
  to(){
    wx.switchTab({
      url: '../HistoricalRecords/HistoricalRecords',
    })
  },
  hidemod(){
    that.setData({showmod:!1})
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})